Page({

  /**
   * 页面的初始数据
   */
  data: {
    schema:[
    ],
    zh: '',
    title:'',
    remark: '',
    webUrl: '',
    formData:{
    },
    result: ''
  },
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.title,
    })

    this.setData({
      apiType: options.zh,
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
  },
  getFormData(e) {
  },
  verifyCopyFromChannel(audioBuffer, channelIndex, copiedArray, startInChannel = 0) {
    // 基本参数验证
    // if (!(audioBuffer instanceof AudioBuffer)) {
    //     return {
    //         success: false,
    //         message: '无效的AudioBuffer实例',
    //         details: null
    //     };
    // }

    if (typeof channelIndex !== 'number' || !Number.isInteger(channelIndex) || 
        channelIndex < 0 || channelIndex >= audioBuffer.numberOfChannels) {
        return {
            success: false,
            message: `无效的声道索引: ${channelIndex}`,
            details: null
        };
    }

    if (!(copiedArray instanceof Float32Array)) {
        return {
            success: false,
            message: '复制的数组必须是Float32Array实例',
            details: null
        };
    }

    if (startInChannel < 0 || startInChannel >= audioBuffer.length) {
        return {
            success: false,
            message: `无效的起始位置: ${startInChannel}`,
            details: null
        };
    }

    // 获取源数据
    const sourceData = audioBuffer.getChannelData(channelIndex);
    const maxCheckLength = Math.min(
        copiedArray.length, 
        sourceData.length - startInChannel
    );

    // 存储不匹配的位置信息
    const mismatches = [];
    const sampleTolerance = 1e-6; // 浮点数比较的容差

    // 逐样本比较
    for (let i = 0; i < maxCheckLength; i++) {
        const sourceValue = sourceData[startInChannel + i];
        const copiedValue = copiedArray[i];
        
        // 检查两个浮点数是否在容差范围内相等
        if (Math.abs(sourceValue - copiedValue) > sampleTolerance) {
            mismatches.push({
                position: i,
                sourceValue: sourceValue,
                copiedValue: copiedValue,
                difference: Math.abs(sourceValue - copiedValue)
            });

            // 限制最多记录10个不匹配项，避免性能问题
            if (mismatches.length >= 10) break;
        }
    }

    // 构建验证结果
    if (mismatches.length === 0) {
        return {
            success: true,
            message: `数据复制成功，共验证了 ${maxCheckLength} 个样本`,
            details: {
                checkedSamples: maxCheckLength,
                totalSamples: sourceData.length,
                startPosition: startInChannel,
                mismatches: 0
            }
        };
    } else {
        return {
            success: false,
            message: `数据复制失败，发现 ${mismatches.length} 处不匹配（最多显示10处）`,
            details: {
                checkedSamples: maxCheckLength,
                totalSamples: sourceData.length,
                startPosition: startInChannel,
                mismatches: mismatches.length,
                sampleMismatches: mismatches
            }
        };
    }
},
  choose1() {  
    try {
      const audioContext = wx.createWebAudioContext()
      const sampleRate = 44100;
      const duration = 1; // 1秒的音频

      // 创建一个单声道音频缓冲区
      const audioBuffer = audioContext.createBuffer(1, sampleRate * duration, sampleRate);
      const sourceData = audioBuffer.getChannelData(0);

      // 填充测试数据（简单的正弦波）
      for (let i = 0; i < sourceData.length; i++) {
          sourceData[i] = Math.sin(220 * 2 * Math.PI * i / sampleRate);
      }

      // 测试1：正常复制整个声道
      const fullCopy = new Float32Array(sourceData.length);
      audioBuffer.copyFromChannel(fullCopy, 0);
      const result1 = this.verifyCopyFromChannel(audioBuffer, 0, fullCopy);
      console.log('测试1（完整复制）:', result1);
      this.setData({
        result: result1
      })
      // // 测试2：部分复制
      // const startPos = 1000;
      // const partialCopy = new Float32Array(2000);
      // audioBuffer.copyFromChannel(partialCopy, 0, startPos);
      // const result2 = this.verifyCopyFromChannel(audioBuffer, 0, partialCopy, startPos);
      // console.log('测试2（部分复制）:', result2);

      // // 测试3：故意修改数据后验证（应该失败）
      // const modifiedCopy = new Float32Array(sourceData.length);
      // audioBuffer.copyFromChannel(modifiedCopy, 0);
      // modifiedCopy[500] = 100; // 故意修改一个值
      // const result3 = this.verifyCopyFromChannel(audioBuffer, 0, modifiedCopy);
      // console.log('测试3（修改后验证）:', result3);

  } catch (error) {
      console.error('测试出错:', error);
  }
  },
})